package org.jeecg.modules.pipe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.pipe.entity.WaterPlant;
import org.jeecg.modules.pipe.service.IWaterPlantService;
import org.jeecg.modules.pipe.vo.PipeMarkerVo;
import org.jeecg.modules.pipe.vo.PipeQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 水厂
 * @Author: jeecg-boot
 * @Date:   2024-05-14
 * @Version: V1.0
 */
@Api(tags="水厂")
@RestController
@RequestMapping("/pipe/waterPlant")
@Slf4j
public class WaterPlantController extends JeecgController<WaterPlant, IWaterPlantService> {
	@Autowired
	private IWaterPlantService waterPlantService;

	@Autowired
	private ISysBaseAPI sysBaseAPI;
	@Autowired
	private RedisUtil redisUtil;
	
	/**
	 * 分页列表查询
	 *
	 * @param waterPlant
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "水厂-分页列表查询")
	@ApiOperation(value="水厂-分页列表查询", notes="水厂-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<WaterPlant>> queryPageList(WaterPlant waterPlant,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<WaterPlant> queryWrapper = QueryGenerator.initQueryWrapper(waterPlant, req.getParameterMap());
		Page<WaterPlant> page = new Page<WaterPlant>(pageNo, pageSize);
		IPage<WaterPlant> pageList = waterPlantService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param waterPlant
	 * @return
	 */
	@AutoLog(value = "水厂-添加")
	@ApiOperation(value="水厂-添加", notes="水厂-添加")
	// @RequiresPermissions("pipe:iot_pipe_water_plant:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody WaterPlant waterPlant) {
		waterPlantService.save(waterPlant);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param waterPlant
	 * @return
	 */
	@AutoLog(value = "水厂-编辑")
	@ApiOperation(value="水厂-编辑", notes="水厂-编辑")
	// @RequiresPermissions("pipe:iot_pipe_water_plant:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody WaterPlant waterPlant) {
		waterPlantService.updateById(waterPlant);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "水厂-通过id删除")
	@ApiOperation(value="水厂-通过id删除", notes="水厂-通过id删除")
	// @RequiresPermissions("pipe:iot_pipe_water_plant:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		waterPlantService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "水厂-批量删除")
	@ApiOperation(value="水厂-批量删除", notes="水厂-批量删除")
	// @RequiresPermissions("pipe:iot_pipe_water_plant:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.waterPlantService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "水厂-通过id查询")
	@ApiOperation(value="水厂-通过id查询", notes="水厂-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<WaterPlant> queryById(@RequestParam(name="id",required=true) String id) {
		WaterPlant waterPlant = waterPlantService.getById(id);
		if(waterPlant==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(waterPlant);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param waterPlant
    */
    // @RequiresPermissions("pipe:iot_pipe_water_plant:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WaterPlant waterPlant) {
        return super.exportXls(request, waterPlant, WaterPlant.class, "水厂");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    // @RequiresPermissions("pipe:iot_pipe_water_plant:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WaterPlant.class);
    }


	/**
	 * 获取水厂列表查询
	 *
	 * @param queryVo
	 * @return
	 */
	//@AutoLog(value = "水厂信息表-分页列表查询")
	@ApiOperation(value="水厂列表查询", notes="水厂列表查询")
	@GetMapping(value = "/api/getList")
	public Result<List<PipeMarkerVo>> getList(PipeQueryVo queryVo) {
		List<PipeMarkerVo> list = waterPlantService.getWaterPlantMarker(queryVo);
		return Result.OK(list);
	}

	/**
	 * 通过id获取水厂接口
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "水厂-通过id查询")
	@ApiOperation(value="水厂-通过id查询", notes="水厂-通过id查询")
	@GetMapping(value = "/api/queryPlant/{id}")
	public Result<WaterPlant> queryPlant(@PathVariable("id") String id) {
		WaterPlant waterPlant = waterPlantService.getById(id);
		if(waterPlant == null) {
			return Result.error("未找到对应数据");
		}

		String redisKey = String.format("sys:cache:dict::%s:%s", "town_category", waterPlant.getTownCategory());
		try {
			String townCategory =  oConvertUtils.getString(redisUtil.get(redisKey));
			waterPlant.setTownCategory(townCategory);
			redisKey = String.format("sys:cache:dict::%s:%s", "plant_category", waterPlant.getPlantCategory());
			String plantCategory =  oConvertUtils.getString(redisUtil.get(redisKey));
			waterPlant.setPlantCategory(plantCategory);
			redisKey = String.format("sys:cache:dict::%s:%s", "processing_level", waterPlant.getProcessingLevel());
			String processingLevel =  oConvertUtils.getString(redisUtil.get(redisKey));
			waterPlant.setProcessingLevel(processingLevel);

			List<String> strings = sysBaseAPI.loadCategoryDictItem(waterPlant.getBasin());
			if(null != strings && strings.size() > 0){
				waterPlant.setBasin(strings.get(0).toString());
			}
		} catch (Exception e) {
			log.warn(e.getMessage(), e);
		}
		return Result.OK(waterPlant);
	}

}
